import _ from 'lodash';
import { routerRedux } from 'dva/router';
import moment from 'moment';
import * as bonusService from '../services/bonusService';

export default {
  namespace: 'bonus',
  state: {
    bonusList: [],
    myBonus: {},
    vipList: [],
  },
  effects: {
    * fetchBonusList({ payload: { branchId, month } }, { call, put, select }) {
      const { shopId, branchNo } = yield select(state => state.auth.currentUser);
      const result = yield call(bonusService.bonusList, shopId, branchId || branchNo, month);
      yield put({ type: 'saveBonusList', payload: result.data });
    },
    * fetchMyBonus({ payload: { month } }, { call, put, select }) {
      const { shopId, branchNo, salemanId } = yield select(state => state.auth.currentUser);
      const result = yield call(bonusService.myBonus, shopId, branchNo, salemanId, month);
      yield put({ type: 'saveMyBonus', payload: result.data });
    },
    * fetchVipList({ payload: { month, keywords } }, { call, put, select }) {
      const { shopId, branchNo, salemanId } = yield select(state => state.auth.currentUser);
      const result = yield call(bonusService.bonusVipList, shopId, branchNo, salemanId, month, keywords);
      yield put({ type: 'saveVipList', payload: result.data });
    },
  },
  reducers: {
    saveBonusList(state, { payload }) {
      return { ...state, bonusList: payload };
    },
    saveMyBonus(state, { payload }) {
      return { ...state, myBonus: payload };
    },
    saveVipList(state, { payload }) {
      return { ...state, vipList: payload };
    },
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname, query }) => {

      });
    },
  },
};
